<template>
  <div>
    <!-- 添加按钮 -->
    <el-button type="primary" icon="el-icon-plus" @click="addTradeMark"
      >添加</el-button
    >
    <!-- 表格 -->
    <el-table style="width: 100%; margin: 30px 0" border :data="records">
      <el-table-column
        prop="prop"
        label="序号"
        width="80px"
        type="index"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="tmName"
        label="品牌名称"
        width="width"
      ></el-table-column>
      <el-table-column label="品牌logo" width="width">
        <template slot-scope="{ row, index }">
          <img :src="row.logoUrl" style="width: 100px; height: 100px" />
        </template>
      </el-table-column>
      <el-table-column prop="prop" label="操作" width="width">
        <template>
          <el-button type="warning" size="mini" icon="el-icon-edit"
            >修改</el-button
          >
          <el-button type="danger" size="mini" icon="el-icon-delete"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分液器 -->
    <el-pagination
      style="textAlign: center"
      :current-page="page"
      :page-size="limit"
      :page-sizes="[3, 5, 10]"
      :total="total"
      layout="prev, pager, next, jumper,->,sizes,total "
      @size-change="handleSizeChange"
      @current-change="getData"
    >
    </el-pagination>
    <!-- 对话框 -->
    <el-dialog title="添加品牌" :visible.sync="dialogFormVisible">
      <el-form style="width: 80%">
        <el-form-item label="品牌名称" label-width="80px" :model="tmForm">
          <el-input autocomplete="off" v-model="tmForm.tmName"></el-input>
        </el-form-item>
        <el-form-item label="品牌logo" label-width="80px">
          <el-upload
            class="avatar-uploader"
            action="/dev-api/admin/product/fileUpload"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
            :show-file-list="false"
          >
            <img v-if="tmForm.logoUrl" :src="tmForm.logoUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            <div class="el-upload__tip" slot="tip">
              只能上传jpg/png文件，且不超过500kb
            </div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addOrUpdateTradeMark"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Trademark",
  data() {
    return {
      // 初始化数据
      page: 1,
      limit: 3,
      total: 0,
      records: [],
      dialogFormVisible: false, //控制对话框的显示与隐藏
      // imgUrl: "",
      tmForm:{
        tmName:'',
        logoUrl:''
      }
    };
  },
  mounted() {
    this.getData();
  },
  methods: {
    // 获取品牌列表数据
    async getData(pages = 1) {
      const { page, limit } = this;
      this.page = pages;
      let result = await this.$API.trademark.reqTradeMarkList(page, limit);
      // console.log(result );
      if (result.code == 200) {
        this.total = result.data.total;
        this.records = result.data.records;
      }
    },
    // 当前页发生变化
    // handleCurrentChange(page){
    //   this.page = page
    //   this.getData()
    // }
    // 当每页展示条数发生变化
    handleSizeChange(limit) {
      this.limit = limit;
      this.getData();
    },
    // 添加按钮的回调
    addTradeMark() {
      this.dialogFormVisible = true;
    },
    // 上传图片相关
    handleAvatarSuccess(res, file) {
      // 收集图片信息
      // this.imgUrl = res.data;
      this.tmForm.logoUrl = res.data;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    // 点击对话框中的确定按钮
    async addOrUpdateTradeMark(){
      this.dialogFormVisible = false
      // 发请求 添加产品
      let result = await this.$API.trademark.reqAddOrUpdateTradeMark(this.tmForm)
      if(result.code == 200){
        this.$message({
          showClose: true,
          message:this.tmForm.id?'修改成功':'新增成功',
          type: 'success'
        });
        this.getData()
      }
    }
  },
};
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>